﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Web;
namespace Super.Common.Files
{
    public class FileOper
    {

        /// <summary>
        /// 路径转换（转换成绝对路径）
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string WebPathTran(string path)
        {
            try
            {
                return HttpContext.Current.Server.MapPath(path);
            }
            catch
            {
                return path;
            }
        }

        /// <summary>
        /// 文件存在性
        /// </summary>
        /// <returns></returns>
        public bool FileExist(string filefullname)
        {
            string filepath = filefullname.Substring(0, filefullname.LastIndexOf("/") + 1);
            string filename = filefullname.Remove(0, filepath.Length);
            string Fileparentpath = WebPathTran(filepath);
            return File.Exists(Fileparentpath + filename);
        }


        public static string GetFileName(string webfilename)
        {
            try
            {
                int lastslash = webfilename.LastIndexOf('/');
                string filename = webfilename.Substring(lastslash + 1);
                return filename;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 上传文件（默认文件名）
        /// </summary>
        /// <param name="fileUploadControl">上传控件ID</param>
        /// <param name="SavePath">指定目录</param>
        /// <param name="IsTimeName">启用时间重命名</param>
        /// <returns></returns>
        public bool UploadFile(FileUpload fileUploadControl, string SavePath, bool IsTimeName)
        {
            string filename = fileUploadControl.FileName;
            if (IsTimeName)
            {
                //时间+扩展名
                filename = DateTime.Now.Ticks.ToString() + filename.Substring(filename.LastIndexOf("."));
            }
            try
            {

                string FileInServerName = WebPathTran(SavePath) + filename;
                fileUploadControl.SaveAs(FileInServerName);
                return true;
            }
            catch (Exception)
            {
                return false;
            }


        }

        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="parentpath">所在目录</param>
        /// <param name="filename">文件名</param>
        /// <param name="httpcontext"></param>
        /// <returns></returns>
        public static void DownLoadFile(string parentpath, string filename, HttpContext httpcontext)
        {
            string Filepath = WebPathTran(parentpath);
            HttpResponse response = httpcontext.Response;
            HttpRequest request = httpcontext.Request;
            if (File.Exists(Filepath + filename))
            {

                response.ContentType = "application/octet-stream";
                response.AddHeader("Content-Disposition", "attachment;filename=" + httpcontext.Server.UrlEncode(filename));
                response.WriteFile(Filepath + filename);
            }
            else
            {
                response.Redirect(request.ServerVariables["HTTP_REFERER"]);
            }

        }
        /// <summary>
        ///  小文件下载
        /// </summary>
        /// <param name="filefullname">完整文件名</param>
        public static void DownLoadSmallFile(string filefullname, HttpContext httpcontext)
        {
            HttpResponse response = httpcontext.Response;
            HttpRequest request = httpcontext.Request;
            string filepath = filefullname.Substring(0, filefullname.LastIndexOf("/") + 1);
            string filename = filefullname.Remove(0, filepath.Length);
            string Fileparentpath = WebPathTran(filepath);
            if (File.Exists(Fileparentpath + filename))
            {

                response.ContentType = "application/octet-stream";
                response.AddHeader("Content-Disposition", "attachment;filename=" + httpcontext.Server.UrlEncode(filename));
                response.TransmitFile(Fileparentpath + filename);
            }
            else
            {
                response.Redirect(request.ServerVariables["HTTP_REFERER"]);
            }

        }


        /// <summary>
        /// 备份文件
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目标文件名</param>
        /// <param name="overwrite">当目标文件存在时是否覆盖</param>
        /// <returns>操作是否成功</returns>
        public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)
        {
            if (!System.IO.File.Exists(sourceFileName))
            {
                throw new FileNotFoundException(sourceFileName + "文件不存在！");
            }
            if (!overwrite && System.IO.File.Exists(destFileName))
            {
                return false;
            }
            try
            {
                System.IO.File.Copy(sourceFileName, destFileName, true);
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 备份文件,当目标文件存在时覆盖
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目标文件名</param>
        /// <returns>操作是否成功</returns>
        public static bool BackupFile(string sourceFileName, string destFileName)
        {
            return BackupFile(sourceFileName, destFileName, true);
        }


        /// <summary>
        /// 恢复文件
        /// </summary>
        /// <param name="backupFileName">备份文件名</param>
        /// <param name="targetFileName">要恢复的文件名</param>
        /// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param>
        /// <returns>操作是否成功</returns>
        public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName)
        {
            try
            {
                if (!System.IO.File.Exists(backupFileName))
                {
                    throw new FileNotFoundException(backupFileName + "文件不存在！");
                }
                if (backupTargetFileName != null)
                {
                    if (!System.IO.File.Exists(targetFileName))
                    {
                        throw new FileNotFoundException(targetFileName + "文件不存在！无法备份此文件！");
                    }
                    else
                    {
                        System.IO.File.Copy(targetFileName, backupTargetFileName, true);
                    }
                }
                System.IO.File.Delete(targetFileName);
                System.IO.File.Copy(backupFileName, targetFileName);
            }
            catch (Exception e)
            {
                throw e;
            }
            return true;
        }

        public static bool RestoreFile(string backupFileName, string targetFileName)
        {
            return RestoreFile(backupFileName, targetFileName, null);
        }


        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="parentPath">所在父级目录</param>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public static bool DeleteFile(string parentPath, string filename)
        {
            try
            {
                string filefullname = WebPathTran(parentPath) + filename;
                File.Delete(filefullname);
                return true;
            }
            catch
            {

                return false;
            }



        }
        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="FileFullName">物理路径文件名</param>
        /// <returns></returns>
        public static bool DeleteFile(string FileFullName)
        {
            FileInfo fi = new FileInfo(FileFullName);
            if (fi.Exists)
            {
                try
                {
                    fi.Delete();
                    return true;

                }
                catch
                {
                    return false;
                }
            }
            return true;
        }


        #region 创建文件夹
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="Path"></param>
        public static void FolderCreate(string Path)
        {
            // 判断目标目录是否存在如果不存在则新建之
            if (!Directory.Exists(Path))
                Directory.CreateDirectory(Path);
        }

        #endregion

        #region 创建目录
        public static void FileCreate(string Path)
        {
            FileInfo CreateFile = new FileInfo(Path); //创建文件 
            if (!CreateFile.Exists)
            {
                FileStream FS = CreateFile.Create();
                FS.Close();
            }
        }
        #endregion

        #region 递归删除文件夹目录及文件
        /****************************************
         * 函数名称：DeleteFolder 
         * 功能说明：递归删除文件夹目录及文件 
         * 参    数：dir:文件夹路径 
         * 调用示列： 
         *           string dir = Server.MapPath( "test/"); 
         *           EC.FileObj.DeleteFolder(dir);       
        *****************************************/
        /// <summary>
        /// 递归删除文件夹目录及文件 
        /// </summary>
        /// <param name="dir"></param> 
        /// <returns></returns>
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件                        
                    else
                        DeleteFolder(d); //递归删除子文件夹 
                }
                Directory.Delete(dir, true); //删除已空文件夹                 
            }
        }

        #endregion

        #region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
        /****************************************
         * 函数名称：CopyDir 
         * 功能说明：将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。 
         * 参    数：srcPath:原始路径,aimPath:目标文件夹 
         * 调用示列： 
         *           string srcPath = Server.MapPath( "test/"); 
         *           string aimPath = Server.MapPath( "test1/");
         *           EC.FileObj.CopyDir(srcPath,aimPath);   
        *****************************************/
        /// <summary>
        /// 指定文件夹下面的所有内容copy到目标文件夹下面 
        /// </summary>
        /// <param name="srcPath">原始路径</param>
        /// <param name="aimPath">目标文件夹</param>
        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之 
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之 
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表，该里面是包含文件以及目录路径的一个数组 
                //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法 
                //string[] fileList = Directory.GetFiles(srcPath); 
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                //遍历所有的文件和目录 
                foreach (string file in fileList)
                {
                    //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件

                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    //否则直接Copy文件 
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.ToString());
            }
        }
        #endregion

        #region 获取指定文件夹下所有子目录及文件(树形)
        /****************************************
         * 函数名称：GetFoldAll(string Path) 
         * 功能说明：获取指定文件夹下所有子目录及文件(树形) 
         * 参    数：Path:详细路径 
         * 调用示列： 
         *           string strDirlist = Server.MapPath( "templates");       
         *           this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist); 
        *****************************************/
        /// <summary>
        /// 获取指定文件夹下所有子目录及文件 
        /// </summary>
        /// <param name="Path">详细路径</param>
        public static string GetFoldAll(string Path)
        {

            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str);
            return str;

        }

        /// <summary>
        /// 获取指定文件夹下所有子目录及文件函数
        /// </summary>
        /// <param name="theDir">指定目录</param>
        /// <param name="nLevel">默认起始值,调用时,一般为0</param>
        /// <param name="Rn">用于迭加的传入值,一般为空</param>
        /// <returns></returns>
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//递归目录 文件 
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 
            foreach (DirectoryInfo dirinfo in subDirectories)
            {

                if (nLevel == 0)
                {
                    Rn += "├";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        _s += "│&nbsp;";
                    }
                    Rn += _s + "├";
                }
                Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";
                FileInfo[] fileInfo = dirinfo.GetFiles();   //目录下的文件 
                foreach (FileInfo fInfo in fileInfo)
                {
                    if (nLevel == 0)
                    {
                        Rn += "│&nbsp;├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            _f += "│&nbsp;";
                        }
                        Rn += _f + "│&nbsp;├";
                    }
                    Rn += fInfo.Name.ToString() + " <br />";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);


            }
            return Rn;
        }

        /****************************************
         * 函数名称：GetFoldAll(string Path)
         * 功能说明：获取指定文件夹下所有子目录及文件(下拉框形) 
         * 参    数：Path:详细路径 
         * 调用示列： 
         *            string strDirlist = Server.MapPath( "templates");      
         *            this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist, "tpl","");
        *****************************************/
        /// <summary>
        /// 获取指定文件夹下所有子目录及文件(下拉框形) 
        /// </summary>
        /// <param name="Path">详细路径</param>
        /// <param name="DropName">下拉列表名称</param>
        /// <param name="tplPath">默认选择模板名称</param>
        public static string GetFoldAll(string Path, string DropName, string tplPath)
        {
            string strDrop = "<select name=\"" + DropName + "\" id=\"" + DropName + "\"><option value=\"\">--请选择详细模板--</option>";
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str, tplPath);
            return strDrop + str + "</select>";

        }

        /// <summary>
        /// 获取指定文件夹下所有子目录及文件函数
        /// </summary>
        /// <param name="theDir">指定目录</param>
        /// <param name="nLevel">默认起始值,调用时,一般为0</param>
        /// <param name="Rn">用于迭加的传入值,一般为空</param>
        /// <param name="tplPath">默认选择模板名称</param>
        /// <returns></returns>
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn, string tplPath)//递归目录 文件 
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录

            foreach (DirectoryInfo dirinfo in subDirectories)
            {

                Rn += "<option value=\"" + dirinfo.Name.ToString() + "\"";
                if (tplPath.ToLower() == dirinfo.Name.ToString().ToLower())
                {
                    Rn += " selected ";
                }
                Rn += ">";

                if (nLevel == 0)
                {
                    Rn += "┣";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i <= nLevel; i++)
                    {
                        _s += "│&nbsp;";
                    }
                    Rn += _s + "┣";
                }
                Rn += "" + dirinfo.Name.ToString() + "</option>";


                FileInfo[] fileInfo = dirinfo.GetFiles();   //目录下的文件
                foreach (FileInfo fInfo in fileInfo)
                {
                    Rn += "<option value=\"" + dirinfo.Name.ToString() + "/" + fInfo.Name.ToString() + "\"";
                    if (tplPath.ToLower() == fInfo.Name.ToString().ToLower())
                    {
                        Rn += " selected ";
                    }
                    Rn += ">";

                    if (nLevel == 0)
                    {
                        Rn += "│&nbsp;├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i <= nLevel; i++)
                        {
                            _f += "│&nbsp;";
                        }
                        Rn += _f + "│&nbsp;├";
                    }
                    Rn += fInfo.Name.ToString() + "</option>";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);


            }
            return Rn;
        }
        #endregion

        #region 获取文件夹大小
        /****************************************
         * 函数名称：GetDirectoryLength(string dirPath) 
         * 功能说明：获取文件夹大小 
         * 参    数：dirPath:文件夹详细路径 
         * 调用示列： 
         *           string Path = Server.MapPath( "templates"); 
         *           Response.Write(EC.FileObj.GetDirectoryLength(Path));       
        *****************************************/
        /// <summary>
        /// 获取文件夹大小 
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <returns></returns>
        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
                return 0;
            long len = 0;
            DirectoryInfo di = new DirectoryInfo(dirPath);
            foreach (FileInfo fi in di.GetFiles())
            {
                len += fi.Length;
            }
            DirectoryInfo[] dis = di.GetDirectories();
            if (dis.Length > 0)
            {
                for (int i = 0; i < dis.Length; i++)
                {
                    len += GetDirectoryLength(dis[i].FullName);
                }
            }
            return len;
        }
        #endregion

        #region 获取指定文件详细属性
        /****************************************
         * 函数名称：GetFileAttibe(string filePath) 
         * 功能说明：获取指定文件详细属性 
         * 参    数：filePath:文件详细路径 
         * 调用示列： 
         *            string file = Server.MapPath( "robots.txt"); 
         *            Response.Write(EC.FileObj.GetFileAttibe(file));         
        *****************************************/
        /// <summary>
        /// 获取指定文件详细属性 
        /// </summary>
        /// <param name="filePath">文件详细路径</param>
        /// <returns></returns>
        public static string GetFileAttibe(string filePath)
        {
            string str = "";
            FileInfo objFI = new FileInfo(filePath);
            str += "详细路径:" + objFI.FullName + "<br>文件名称:" + objFI.Name + "<br>文件长度:" + objFI.Length + "字节<br>创建时间" + objFI.CreationTime + "<br>最后访问时间:" + objFI.LastAccessTime + "<br>修改时间:" + objFI.LastWriteTime + "<br>所在目录:" + objFI.DirectoryName + "<br>扩展名:" + objFI.Extension;
            return str;
        }
        #endregion 


        #region 读取网络文件
   /// <summary>
   /// 读取网络文件
   /// </summary>
   /// <param name="fileUlr"></param>
   /// <returns></returns>
   public static byte[] getInternetFileContent(string fileUlr)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            MemoryStream msstream = new MemoryStream();

            try
            {
                request = WebRequest.Create(fileUlr) as HttpWebRequest;
                response = request.GetResponse() as HttpWebResponse;
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream ms = response.GetResponseStream();
                        long len = response.ContentLength;
                        byte[] buffer = new byte[1024];
                        long counter = 1024;
                        long mod = len % counter;
                        while (len / counter > 0)
                        {
                            len -= counter;
                            msstream.Write(buffer, 0, buffer.Length);
                        }
                        ms.Read(buffer, 0, (int)mod);
                        msstream.Write(buffer, 0, (int)mod);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
            }
            finally
            {
                request = null;
                response = null;
            }
            return msstream.ToArray();
        }
        #endregion


    }
}
